<template>
    <div class='publish-container'>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <Breadcrumb>发布文章</Breadcrumb>
      </div>
      <el-form ref="form" :model="form"  :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
            <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
         <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
        </el-form-item>
        <el-form-item label="封面" >
            <el-radio-group v-model="form.coverType">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="" v-if="form.coverType > 0">
          <el-row>
            <el-col :span="6" v-for="(item,index) in form.coverType" :key="index">
              {{index}}
              <Cover v-model="form.coverImages[index]"></Cover>
            </el-col>
          </el-row>

        </el-form-item>
        <Channel v-model="form.ChannelId"></Channel>
        <el-form-item>
            <el-button type="primary" @click="hAdd(false)">发表</el-button>
            <el-button @click="hAdd(true)">存入草稿</el-button>
        </el-form-item>
        </el-form>
    </el-card>
    </div>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'

export default {
  components: {
    quillEditor
  },
  name: 'AddArticle',
  data () {
    return {
      form: {
        title: '',
        content: '',
        coverType: 0,
        ChannelId: '',
        coverImages: [] // 用于保存封面图片的地址
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        ChannelId: [
          { required: true, message: '请选择文章频道', trigger: 'blur' }
        ]
      },
      editorOption: {
        placeholder: '',
        modules: {
        // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      }
    }
  },
  methods: {
    async hAdd (isDraft) {
      console.log(this.form)
      // 验证信息
      this.$refs.form.validate(result => {
        console.log(result)
        // 验证通过
        if (result) {
          this.doAdd(isDraft)
        }
      })
    },
    async doAdd (isDraft) {
      try {
        const result = await this.$http({
          method: 'POST',
          url: '/mp/v1_0/articles?draft=' + isDraft,
          data: {
            title: this.form.title,
            content: this.form.content,
            cover: {
              type: this.form.coverType,
              images: this.form.coverImages
            },
            channel_id: this.form.ChannelId
          }
        })
        console.log(result)
        this.$message.success('操作成功')
        this.form.title = ''
        this.form.content = ''
        this.form.ChannelId = ''
      } catch {
        this.$message.error('操作失败')
      }
    }
  }
}
</script>
<style lang="less">
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
